#pragma once
#include <chrono>
#include "TimeWheel.h"

namespace TimeWheel
{
	template <typename T>
	class CTimeWheelWrapper
	{
	public:
		CTimeWheelWrapper()
		{
			auto tNow = this->Now();
			m_pTimeWheel = TimeWheel::CTimeWheel::CreateNew();
			if (m_pTimeWheel == nullptr)
			{
				return;
			}

			if (!m_pTimeWheel->Create(tNow))
			{
				return;
			}
		}

		~CTimeWheelWrapper()
		{
			if (m_pTimeWheel != nullptr)
			{
				m_pTimeWheel->Release();
				m_pTimeWheel = nullptr;
			}
		}

		bool Run()
		{
			if (m_pTimeWheel == nullptr)
			{
				return false;
			}

			m_pTimeWheel->RunTicks(this->Now());

			return true;
		}

		time_t Now()
		{
			return std::chrono::duration_cast<T>(TWClock::now().time_since_epoch()).count();
		}

		CTimeWheel* TimeWheel() { return m_pTimeWheel; }

	private:
		CTimeWheel* m_pTimeWheel;
		using TWClock = std::chrono::system_clock;
	};

	using TimeWheelMS = CTimeWheelWrapper<std::chrono::milliseconds>;
	using TimeWheelSec = CTimeWheelWrapper<std::chrono::seconds>;
}
